 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sunspotworld;

/**
 *
 * @author andresgodoysalavert
 */
import java.awt.*;

import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.*;

import javax.swing.border.*;

 

public class CosechaDialog extends JDialog {

 

    private JTextField nombre;
    private JComboBox cantidad;
    private JLabel nombreL;
    private JLabel cantidadL;
    private JLabel imagelabel;
    private JButton crear;
    private boolean succeeded;
    ConexionBD con = new ConexionBD();
 

    public CosechaDialog(Frame parent) {

        super(parent, "Nueva Cosecha", true);

        JPanel panel = new JPanel(new GridBagLayout());
        JPanel panel2 = new JPanel(new GridBagLayout());
        GridBagConstraints cs = new GridBagConstraints();
        
        ImageIcon image = new ImageIcon("C:\\Users\\Salaver\\Documents\\NetBeansProjects\\host\\trunk\\HidroponiaDesktop\\img\\logo.png");
        imagelabel = new JLabel(image);
        panel2.add(imagelabel);
        
        cs.fill = GridBagConstraints.HORIZONTAL;
        
        nombreL = new JLabel("Nombre de la Cosecha:  ");
        cs.gridx = 0;
        cs.gridy = 8;
        cs.gridwidth = 1;
        panel.add(nombreL, cs);
 
        nombre = new JTextField(20);
        cs.gridx =1;
        cs.gridy = 8;
        cs.gridwidth = 2;
        panel.add(nombre, cs);
 
        cantidadL = new JLabel("Cantidad de Vegetales:  ");
        cs.gridx = 0;
        cs.gridy = 9;
        cs.gridwidth = 1;
        panel.add(cantidadL, cs);
    
        cantidad = new JComboBox();
        cantidad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1","2","3","4","5","6" }));
        cs.gridx = 1;
        cs.gridy = 9;
        cs.gridwidth = 2;
        panel.add(cantidad, cs);
        panel.setBorder(new LineBorder(Color.GRAY));
 
 
        crear = new JButton("Crear");
 
        crear.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e) {
                try {
                    con.conectar();
                    Date now = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
                    String query1 = "INSERT INTO cosecha VALUES(null,'"+getNombre()+"','"+getCantidad()+"',1)";
                    String query2 = "SELECT MAX(id) FROM cosecha";
                    con.insertar(query1);
                    ResultSet rs1 = con.consultar(query2);
                    rs1.next();
                    String query3 = "INSERT INTO etapa_cosecha VALUES(1,"+rs1.getInt(1)+",'"+sdf.format(now.getTime())+"',null)";
                    String query4 = "INSERT INTO semillero VALUES(null,null,"+rs1.getInt(1)+")";
                    con.insertar(query3);
                    con.insertar(query4);
                    String query5 = "SELECT MAX(id) FROM semillero";
                    String query6 = "SELECT * FROM etapa WHERE id=1";
                    ResultSet rs2 = con.consultar(query5);
                    ResultSet rs3 = con.consultar(query6);
                    rs2.next();
                    rs3.next();
                    HidroponiaHost.cosecha.etapa.setId(rs3.getInt(1));
                    HidroponiaHost.cosecha.etapa.setNombre(rs3.getString(2));
                    HidroponiaHost.cosecha.etapa.setDuracion(rs3.getInt(3));
                    HidroponiaHost.cosecha.etapa.setCiclo(rs3.getInt(4));
                    HidroponiaHost.cosecha.etapa.setFase(rs3.getInt(5));
                    HidroponiaHost.cosecha.setCantidad(getCantidad());
                    HidroponiaHost.cosecha.setId(rs1.getInt(1));
                    HidroponiaHost.cosecha.setNombre(getNombre());
                    HidroponiaHost.cosecha.lechuga.setId(1);
                    HidroponiaHost.cosecha.lechuga.setTipo("americano");
                    HidroponiaHost.cosecha.semillero = new Semillero();
                    HidroponiaHost.cosecha.semillero.setId(rs2.getInt(1));
                    HidroponiaHost.cosecha.semillero.setCantidad(null);
                    con.desconectar();
                    dispose();
                } catch (SQLException ex) {
                    Logger.getLogger(CosechaDialog.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(HidroponiaHost.bienvenido,
                    "Error, cosecha ya existente, eliga un nombre nuevo.",
                    "Atencion",
                    JOptionPane.ERROR_MESSAGE);
                }
            }
        });
       
        JPanel bp = new JPanel();
        bp.add(crear);
        //bp.add(btnCancel);
 
        getContentPane().add(panel2, BorderLayout.PAGE_START);
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(bp, BorderLayout.PAGE_END);
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        pack();
        setResizable(false);
        setLocationRelativeTo(parent);
    }

 
    public String getNombre() {
        return nombre.getText().trim();
    }
    
    public String getCantidad() {
        return cantidad.getSelectedItem().toString().trim();
    }

 
    public boolean isSucceeded() {
        return succeeded;
    }
}
